package base

import (
	"fmt"
)

func Demo() {
	var name string = "张三"
	var age = 15
	sex := 1

	var (
		bobby        = "玩电脑"
		want  string = "ds"
	)

	fmt.Printf("姓名%v,性别%d 年龄%d\n,爱好%v, want%v", name, age, sex, bobby, want)
}

type Direction int

func CreateConst() {
	const (
		a Direction = iota
		b
		c
	)
	fmt.Printf("a:%d,b:%d,b:%d", a, b, c)
}

func FlowController(types int, age int) {
	switch types {
	case 1:
		ifElse(age)
	case 2:
		forMethod(age)
	case 3:
		forRangeMethod()
	case 4:
		gotoMethod()
	case 5:
		stringMethod()
	case 6:
		pointerMethod()
	case 7:
		changeValue()
	case 8:
		arrayMethod()
	case 9:
		structMethod()
	case 10:
		sliceMethod()
	case 11:
		mapMethod()
	}

}

func forRangeMethod() {
	// 声明一个数组
	var ranges = []int{1, 2, 3, 57, 9}
	for key, value := range ranges {
		fmt.Printf("key: %d, value: %d\n", key, value)
	}
	// 声明一个字符串
	str := "张三加油！"
	for key, value := range str {
		fmt.Printf("key: %v, value: %v\n", key, string(value))
	}
	// 声明 map
	mapType := map[string]string{
		"name": "张三",
		"age":  "18",
		"sex":  "男",
	}
	for key, value := range mapType {
		fmt.Printf("key: %v, value: %v\n", key, value)
	}
	// 声明通道
	c := make(chan int)
	// 协程向通道内写入数据
	go func() {
		c <- 7
		c <- 7
		c <- 8
		c <- 9
		// 关闭通道
		close(c)
	}()

	// 遍历通道--通道只能遍历出value
	for value := range c {
		fmt.Printf("value: %v\n", value)
	}
}

func forMethod(age int) {
	for i := 1; i <= age; i++ {
		fmt.Printf("循环次数%d\n", i)
	}
}

func ifElse(age int) {
	if age == 1 {
		print("1")
	} else if age == 2 {
		print("2")
	} else {
		print("nothing")
	}
}

func gotoMethod() {
	var num = 1
	for i := 1; i < 20; i++ {
		for k := 1; k < 20; k++ {
			num++
			fmt.Printf("循环次数%d", num)
			if k > 5 {
				// 跳转到指定标签
				goto breakTag
			}
		}
	}

breakTag:
	fmt.Print("跳转之后的内容")
}

func stringMethod() {
	// 字符串的遍历
	str := "go web"
	// 获取索引为0的字符
	fmt.Println(string(str[0]))

	// 通用字符串遍历
	str = "I love golang"
	chars := []rune(str)
	for _, value := range chars {
		fmt.Println(string(value))
	}

	// 使用 buffer
	// 字符串的修改
	str = "Hi 世界"
	by := []byte(str)
	by[2] = ','
	fmt.Printf("%s\n", str)
	fmt.Printf("%s\n", by)

	bys := []rune(str)
	bys[3] = '中'
	bys[4] = '国'
	fmt.Println(str)
	fmt.Println(string(bys))

}

// 指针
func pointerMethod() {
	// 定义普通类型变量
	a := 1
	// 定义指针类型变量
	var b *int = &a
	c := 2
	fmt.Printf("%p, %p, %p\n", &a, b, &c)
	fmt.Printf("获取指针的值 %d\n", *b)
	fmt.Printf("获取指针的类型 %T\n", *b)
}

func changeValue() {
	a, b := 5, 9
	temp := &a
	tempb := &b

	// 错误示例
	a = *tempb
	b = *temp

	fmt.Printf("a: %p, b:%p", temp, tempb)
	fmt.Printf("a: %d, b:%d", a, b)
}

func arrayMethod() {
	var arr = [...]int{1, 3, 6, 8, 13, 24}
	for key, value := range arr {
		fmt.Printf("key: %v, value: %v\n", key, value)
	}

}

type Book struct {
	title   string
	author  string
	subject string
	price   float32
}

func structMethod() {
	book := Book{
		title:   "golang从入门到放弃",
		author:  "lstink",
		subject: "16个章节",
		price:   13.14,
	}
	fmt.Println(book)
	fmt.Println(book.title)
	book2 := Book{
		title:  "golang从入门到放弃",
		author: "lstink",
	}
	fmt.Println(book2)
	// 结构体赋值
	book2.subject = "18个章节"
	fmt.Println(book2)

	// 结构体指针
	var bookPointer = &Book{}
	fmt.Printf("%p", bookPointer)

}

func sliceMethod() {
	var arr = [5]int{1, 2, 3, 4, 5}
	fmt.Println(arr, arr[:4])
	// 切片
	slices := arr[:]
	fmt.Println(slices)
	// 向切片中追加值
	newSlice := append(slices, 9)
	fmt.Println(newSlice)
	// 切片大小
	fmt.Println(len(newSlice))
	fmt.Println(cap(newSlice))
	// make声明切片--将会发生内存分配操作
	slice1 := make([]int, 6)
	slice2 := make([]int, 6, 10)
	fmt.Println(slice1, slice2)
	fmt.Println(len(slice1), len(slice2))
	fmt.Println(cap(slice1), cap(slice2))

}

func mapMethod() {
	map1 := map[string]int{}
	fmt.Println(map1)
	// 声明赋值
	map1 = map[string]int{"name": 3, "age": 16, "sex": 5}
	fmt.Println(map1)
	// 使用make,创建map
	createMap := make(map[string]string)
	createMap["name"] = "张三"
	createMap["age"] = "18"
	fmt.Println(createMap)
	fmt.Println(createMap["name"])
}
